home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Entertainment / MacMud / Mud 4.0 / mac.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-24  |  8.6 KB  |  374 lines  |  [TEXT/MPS ]

  1. #include <mac.h>
  2. #include "config.h"
  3. #include "rc.h"
  4.  
  5. long gMudLibDirID;
  6. short gMudLibVolRef;
  7. long gMudResponseFactor;
  8.  
  9. #undef fopen
  10. #undef open
  11.  
  12. #define DIR_FLAG 0x10
  13.  
  14. extern char *string_copy(char *str); 
  15.  
  16. /*
  17.  * Very weak replacement for crypt().
  18.  */
  19.  
  20. #define rot(v,c) (((v) << c) | ((v) >> (8-c)))
  21.  
  22. static int mirror(int x)
  23. {
  24.     return ((x & 1) << 7) | ((x & 2) << 5) | ((x & 4) << 3) | ((x & 8) << 1) |
  25.       ((x & 16) >> 1) | ((x & 32) >> 3) | ((x & 64) >> 5) | ((x & 128) >> 7);
  26. }
  27.  
  28. char *crypt(char *pw, char *salt)
  29. {
  30.     static char e64[] =
  31.         "ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz.0123456789";
  32.     static char enc[9];
  33.     char tp[9];
  34.     int scrm,i,bpos,n;
  35.  
  36.     memset(tp,'\0',8);
  37.     strncpy(tp,pw,8);
  38.     tp[0] ^= enc[0] = salt[0];
  39.     tp[3] ^= enc[1] = salt[1];
  40.     scrm = 165;
  41.     for (i = 7; i >= 0; i--) scrm = (tp[i] ^= scrm >> 1);
  42.     for (i = 7; i; i--) tp[tp[i-1] & 7] ^= rot(tp[i],1);
  43.     for (i = (tp[0] & 127)+19; i; i--)
  44.         tp[tp[i & 7] & 7] ^= rot(tp[(tp[i & 7] >> 3) & 7],tp[i & 7] >> 6);
  45.     for (i = 3; i >= 0; i--) tp[i] ^= mirror(tp[7-i]);
  46.     bpos = 0;
  47.     for (i = 7; i > 1; i--) {
  48.         n = ((tp[bpos >> 3] >> (bpos & 7)) | (tp[(bpos >> 3)+1] << (8-(bpos & 7)))) & 63;
  49.         enc[i] = e64[n];
  50.         bpos += 6;
  51.     }
  52.     enc[8] = '\0';
  53.     return enc;
  54. }
  55.  
  56. int random(void)
  57. {
  58.     return (unsigned short)Random();
  59. }
  60.  
  61. char *parsefname(const char *fname, char *namebuff, char *mud_lib)
  62. {
  63.     short i, j = 0;
  64.     Boolean slash = false;
  65.  
  66.     if (mud_lib) {
  67.         j = strlen(MUD_LIB);
  68.         strcpy(namebuff, MUD_LIB);
  69.     }
  70.     if (*fname != '/')
  71.         namebuff[j++] = ':';    
  72.     for (i = 0; i < strlen(fname); i++) {
  73.         if (fname[i] == '/') {
  74.             if (!slash) {
  75.                 namebuff[j++] = ':';
  76.                 slash = true;
  77.             }
  78.         } else if (fname[i] != ':') {
  79.             slash = false;
  80.             namebuff[j++] = fname[i];
  81.         }
  82.     }
  83.     namebuff[j] = '\0';
  84.     return namebuff;
  85. }
  86.  
  87. int macopen (const char *fname, int mode)
  88. {
  89.     char namebuff[64];
  90.  
  91.     return open(parsefname(fname, namebuff, MUD_LIB), mode);
  92. }
  93.  
  94. FILE *macfopen (const char *fname, const char *mode)
  95. {
  96.     char namebuff[64];
  97.  
  98.     return fopen(parsefname(fname, namebuff, MUD_LIB), mode);
  99. }
  100.  
  101. int macrename(const char *oldname, const char *newname)
  102. {
  103.     char newnamebuff[64];
  104.     char oldnamebuff[64];
  105.  
  106.     remove(parsefname(newname, newnamebuff, MUD_LIB));
  107.     return rename(parsefname(oldname, oldnamebuff, MUD_LIB), newnamebuff);
  108. }
  109.  
  110. int macremove(const char *filename)
  111. {
  112.     char namebuff[64];
  113.  
  114.     return remove(parsefname(filename, namebuff, MUD_LIB));
  115. }
  116.  
  117. int stat(char *fname, struct stat *buf) {
  118.     Str255 pstr;
  119.     CInfoPBRec pb;
  120.  
  121.     parsefname(fname, pstr, NULL);
  122.     c2pstr(pstr);
  123.     pb.hFileInfo.ioCompletion = NULL;
  124.     pb.hFileInfo.ioNamePtr = pstr;
  125.     pb.hFileInfo.ioFDirIndex = 0;
  126.     pb.hFileInfo.ioVRefNum = gMudLibVolRef;
  127.     pb.hFileInfo.ioDirID = gMudLibDirID;
  128.     pb.hFileInfo.ioResult = noErr;
  129.     if (PBGetCatInfo(&pb, false) != noErr) {
  130.         errno = ENOENT;
  131.         return -1;
  132.     }
  133.     buf->st_mode = S_IFREG;
  134.     if (pb.hFileInfo.ioFlAttrib & 0x10)
  135.         buf->st_mode = S_IFDIR;
  136.     buf->st_size = pb.hFileInfo.ioFlLgLen;
  137.     buf->st_mtime = pb.hFileInfo.ioFlMdDat;
  138.     return 0;
  139. }
  140.  
  141. DirList *macgetdir(char *pathname)
  142. {
  143.     CInfoPBRec    pb;
  144.     short        i, x;
  145.     Str255        fName;
  146.     short        index = 1;
  147.     long        directoryID;
  148.     char        namebuff[64];
  149.     DirList        *dirList = NULL;
  150.     Boolean        atleastone = false;
  151.  
  152.     directoryID = gMudLibDirID;
  153.     pb.hFileInfo.ioResult = noErr;
  154.     if (pathname[0] != '.') {
  155.         for (i = 0, x = 0; i < strlen(pathname); i++) {
  156.             if (pathname[i] == '/') {
  157.                 namebuff[0] = i - x;
  158.                 memcpy(&namebuff[1], &pathname[x], namebuff[0]);
  159.                 pb.hFileInfo.ioCompletion = NULL;
  160.                 pb.hFileInfo.ioNamePtr = namebuff;
  161.                 pb.hFileInfo.ioVRefNum = gMudLibVolRef;
  162.                 pb.hFileInfo.ioDirID = directoryID;
  163.                 pb.hFileInfo.ioResult = noErr;
  164.                 pb.hFileInfo.ioFDirIndex = 0;
  165.                 if (PBGetCatInfo(&pb, false) != noErr || !(pb.hFileInfo.ioFlAttrib & DIR_FLAG)) {
  166.                     return NULL;
  167.                 }
  168.                 directoryID = pb.hFileInfo.ioDirID;
  169.                 x = i + 1;
  170.                 atleastone = true;
  171.             }
  172.         }
  173.         if (!atleastone) {
  174.             i = strlen(pathname);
  175.         }
  176.         namebuff[0] = i - x;
  177.         memcpy(&namebuff[1], &pathname[x], namebuff[0]);
  178.         pb.hFileInfo.ioCompletion = NULL;
  179.         pb.hFileInfo.ioNamePtr = namebuff;
  180.         pb.hFileInfo.ioVRefNum = gMudLibVolRef;
  181.         pb.hFileInfo.ioDirID = directoryID;
  182.         pb.hFileInfo.ioResult = noErr;
  183.         pb.hFileInfo.ioFDirIndex = 0;
  184.         if (PBGetCatInfo(&pb, false) != noErr || !(pb.hFileInfo.ioFlAttrib & DIR_FLAG)) {
  185.             return NULL;
  186.         }
  187.         directoryID = pb.hFileInfo.ioDirID;
  188.     }
  189.  
  190.     while (pb.hFileInfo.ioResult == noErr) {
  191.         pb.hFileInfo.ioCompletion = NULL;
  192.         pb.hFileInfo.ioNamePtr = fName;
  193.         pb.hFileInfo.ioVRefNum = gMudLibVolRef;
  194.         pb.hFileInfo.ioDirID = directoryID;
  195.         pb.hFileInfo.ioResult = noErr;
  196.         pb.hFileInfo.ioFDirIndex = index;
  197.  
  198.         if (PBGetCatInfo(&pb, false) == noErr) {
  199.             DirList *dirEntry;
  200.  
  201.             fName[*fName + 1] = '\0';
  202.             dirEntry = (DirList *)NewPtr(sizeof(DirList));
  203.             dirEntry->name = string_copy(&fName[1]);
  204.             dirEntry->next = dirList;
  205.             dirList = dirEntry;
  206.         }
  207.         index++;
  208.     }
  209.     return dirList;
  210. }
  211.  
  212. int maccountdir(DirList *dirlist)
  213. {
  214.     int count;
  215.  
  216.     for (count = 0; dirlist; dirlist = dirlist->next, count++)
  217.         ;
  218.     return count;
  219. }
  220.  
  221. void macfreedir(DirList *dirlist)
  222. {
  223.     DirList *dlist;
  224.  
  225.     while (dirlist) {
  226.         dlist = dirlist->next;
  227.         DisposPtr((Ptr)dirlist);
  228.         dirlist = dlist;
  229.     }
  230. }
  231.  
  232. static char fnamebuff[64];
  233.  
  234. char *finddir(char *pathname, long *directoryID)
  235. {
  236.     CInfoPBRec    pb;
  237.     short        i, x;
  238.     Boolean        atleastone = false;
  239.  
  240.     *directoryID = gMudLibDirID;
  241.     for (i = 0, x = 0; i < strlen(pathname); i++) {
  242.         if (pathname[i] == '/') {
  243.             fnamebuff[0] = i - x;
  244.             memcpy(&fnamebuff[1], &pathname[x], fnamebuff[0]);
  245.             pb.hFileInfo.ioCompletion = NULL;
  246.             pb.hFileInfo.ioNamePtr = fnamebuff;
  247.             pb.hFileInfo.ioVRefNum = gMudLibVolRef;
  248.             pb.hFileInfo.ioDirID = *directoryID;
  249.             pb.hFileInfo.ioResult = noErr;
  250.             pb.hFileInfo.ioFDirIndex = 0;
  251.             if (PBGetCatInfo(&pb, false) != noErr ||
  252.                 !(pb.hFileInfo.ioFlAttrib & DIR_FLAG))
  253.             {
  254.                 return fnamebuff;
  255.             }
  256.             x = i + 1;
  257.             atleastone = true;
  258.             *directoryID = pb.hFileInfo.ioDirID;
  259.         }
  260.     }
  261.     if (!atleastone) {
  262.         i = strlen(pathname);
  263.     }
  264.     fnamebuff[0] = i - x;
  265.     memcpy(&fnamebuff[1], &pathname[x], fnamebuff[0]);
  266.     pb.hFileInfo.ioCompletion = NULL;
  267.     pb.hFileInfo.ioNamePtr = fnamebuff;
  268.     pb.hFileInfo.ioVRefNum = gMudLibVolRef;
  269.     pb.hFileInfo.ioDirID = *directoryID;
  270.     pb.hFileInfo.ioResult = noErr;
  271.     pb.hFileInfo.ioFDirIndex = 0;
  272.     if (PBGetCatInfo(&pb, false) != noErr ||
  273.         !(pb.hFileInfo.ioFlAttrib & DIR_FLAG))
  274.     {
  275.         return fnamebuff;
  276.     }
  277.     return NULL;
  278. }
  279.  
  280. /*
  281.  * unix make directory
  282.  */
  283. int mkdir(char *path, int mode) 
  284. {
  285. #pragma unused(mode)
  286.  
  287.     HParamBlockRec    pb;
  288.     char            *nameptr;
  289.     long            directoryID;
  290.  
  291.     if ((nameptr = finddir(path, &directoryID)) != NULL) {
  292.         pb.fileParam.ioCompletion = NULL;
  293.         pb.fileParam.ioNamePtr = nameptr;
  294.         pb.fileParam.ioVRefNum = gMudLibVolRef;
  295.         pb.fileParam.ioDirID = directoryID;
  296.         pb.fileParam.ioResult = noErr;
  297.         pb.fileParam.ioFDirIndex = 0;
  298.         return ((PBDirCreate(&pb, false) != noErr) * -1);
  299.     }
  300.     return (-1);
  301. }
  302.  
  303. int rmdir(char *path)
  304. {
  305.     CInfoPBRec        pb;
  306.     HParamBlockRec    dirpb;
  307.     short            i, x;
  308.     long            directoryID, tid;
  309.     Boolean            atleastone = false;
  310.  
  311.     directoryID = tid = gMudLibDirID;
  312.     for (i = 0, x = 0; i < strlen(path); i++) {
  313.         if (path[i] == '/') {
  314.             fnamebuff[0] = i - x;
  315.             memcpy(&fnamebuff[1], &path[x], fnamebuff[0]);
  316.             pb.hFileInfo.ioCompletion = NULL;
  317.             pb.hFileInfo.ioNamePtr = fnamebuff;
  318.             pb.hFileInfo.ioVRefNum = gMudLibVolRef;
  319.             pb.hFileInfo.ioDirID = tid;
  320.             pb.hFileInfo.ioResult = noErr;
  321.             pb.hFileInfo.ioFDirIndex = 0;
  322.             if (PBGetCatInfo(&pb, false) != noErr ||
  323.                 !(pb.hFileInfo.ioFlAttrib & DIR_FLAG))
  324.             {
  325.                 return (-1);
  326.             }
  327.             x = i + 1;
  328.             atleastone = true;
  329.             directoryID = tid;
  330.             tid = pb.hFileInfo.ioDirID;
  331.         }
  332.     }
  333.     if (!atleastone) {
  334.         i = strlen(path);
  335.     }
  336.     directoryID = tid;
  337.     fnamebuff[0] = i - x;
  338.     memcpy(&fnamebuff[1], &path[x], fnamebuff[0]);
  339.     pb.hFileInfo.ioCompletion = NULL;
  340.     pb.hFileInfo.ioNamePtr = fnamebuff;
  341.     pb.hFileInfo.ioVRefNum = gMudLibVolRef;
  342.     pb.hFileInfo.ioDirID = directoryID;
  343.     pb.hFileInfo.ioResult = noErr;
  344.     pb.hFileInfo.ioFDirIndex = 0;
  345.     if (PBGetCatInfo(&pb, false) != noErr ||
  346.         !(pb.hFileInfo.ioFlAttrib & DIR_FLAG))
  347.     {
  348.         return (-1);
  349.     }
  350.     dirpb.fileParam.ioCompletion = NULL;
  351.     dirpb.fileParam.ioNamePtr = fnamebuff;
  352.     dirpb.fileParam.ioVRefNum = gMudLibVolRef;
  353.     dirpb.fileParam.ioDirID = directoryID;
  354.     dirpb.fileParam.ioResult = noErr;
  355.     dirpb.fileParam.ioFDirIndex = 0;
  356.     return ((PBHDelete(&dirpb, false) != noErr) * -1);
  357. }
  358.  
  359. static short time_secs;
  360. static unsigned long time_ticks;
  361.  
  362. void start_timer(short seconds)
  363. {
  364.     time_secs = seconds;
  365.     time_ticks = TickCount() + (unsigned long)time_secs * (unsigned long)60;
  366. }
  367.  
  368. int timer_expired(void)
  369. {
  370.     if (TickCount() >= time_ticks) {
  371.         time_ticks = TickCount() + (unsigned long)time_secs * (unsigned long)60;
  372.         return 1;
  373.     } else return 0;
  374. }